package com.example.wx.dailyreport.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.wx.dailyreport.mapper.WordsupportMapper;
import com.example.wx.dailyreport.mapper.WxwordMapper;
import com.example.wx.dailyreport.pojo.Wordsupport;
import com.example.wx.dailyreport.service.WordsupportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hulinbo
 * @since 2020-08-08
 */
@Service
@Transactional
public class WordsupportServiceImpl extends ServiceImpl<WordsupportMapper, Wordsupport> implements WordsupportService {

    @Autowired
    private WordsupportMapper wordsupportMapper;

    @Autowired
    private WxwordMapper wxwordMapper;

    @Override
    public boolean updateWordsupport(Integer flag, String username, Integer word_id) {
        int add = 0;
        QueryWrapper<Wordsupport> updateWrapper = new QueryWrapper<>();
        updateWrapper.lambda().eq(Wordsupport::getUsername, username).eq(Wordsupport::getWordId, word_id);
        if (wordsupportMapper.selectOne(updateWrapper) != null) {
            wordsupportMapper.delete(updateWrapper);
            add = -1;
        } else {
            Wordsupport wordsupport = new Wordsupport();
            wordsupport.setUsername(username);
            wordsupport.setWordId(word_id);
            wordsupportMapper.insert(wordsupport);
            add = 1;
        }

        return wxwordMapper.updateSupportnumById(add, word_id) > 0;
    }

}
